package com.example.consulclient8889.controller;

import com.example.consulclient8889.feign.FeignUserClient;
import entity.TestDemo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ConcurrentHashMap;

@RestController
@Slf4j
public class DemoController {

    @Value("${server.port}")
    private int port;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private FeignUserClient feignUserClient ;


    @GetMapping("/bconsul/test")
    public String test() {
        //RestTemplate restTemplate = new RestTemplate();
        // String forObject = restTemplate.getForObject("http://localhost:8899/user/infos", String.class);
        //List<ServiceInstance> user = discoveryClient.getInstances("user8899");
        //ServiceInstance user8899 = loadBalancerClient.choose("user8899");
        //String forObject = restTemplate.getForObject("http://user8899/user/infos", String.class);
        String forObject = feignUserClient.test();
        log.info(forObject+"  | 调用openFeign！");
        return "bconsul: "+forObject;
    }

    @GetMapping("/bconsul/infos_id")
    public String test2(int id) {
        //RestTemplate restTemplate = new RestTemplate();
        // String forObject = restTemplate.getForObject("http://localhost:8899/user/infos", String.class);
        //List<ServiceInstance> user = discoveryClient.getInstances("user8899");
        //ServiceInstance user8899 = loadBalancerClient.choose("user8899");
        //String forObject = restTemplate.getForObject("http://user8899/user/infos", String.class);
        String forObject = feignUserClient.testid(id);
        log.info(forObject+"  | bconsul调用openFeign！");
        return forObject+"|bconsul";
    }

    @PostMapping("/bconsul/test")
    public ConcurrentHashMap test23(@RequestBody TestDemo demo) {
        log.info(demo.toString());
        //RestTemplate restTemplate = new RestTemplate();
        // String forObject = restTemplate.getForObject("http://localhost:8899/user/infos", String.class);
        //List<ServiceInstance> user = discoveryClient.getInstances("user8899");
        //ServiceInstance user8899 = loadBalancerClient.choose("user8899");
        //String forObject = restTemplate.getForObject("http://user8899/user/infos", String.class);
       return feignUserClient.demopost(demo);
    }




    @GetMapping("/bconsul/infos")
    public String test2333(int id) {
        //RestTemplate restTemplate = new RestTemplate();
        // String forObject = restTemplate.getForObject("http://localhost:8899/user/infos", String.class);
        //List<ServiceInstance> user = discoveryClient.getInstances("user8899");
        //ServiceInstance user8899 = loadBalancerClient.choose("user8899");
        //String forObject = restTemplate.getForObject("http://user8899/user/infos", String.class);
        return port +"|bconsul";
    }

}
